<!-- 添加地址-对话框组件 -->
<template>
    <XtxDialog :title="(formData.id?'编辑':'添加')+'收货地址'" v-model:visible="visibleDialog">
      <div class="address-edit">
        <div class="xtx-form">
      <div class="xtx-form-item">
        <div class="label">收货人：</div>
        <div class="field">
          <input v-model="formData.receiver" class="input" placeholder="请输入收货人" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">手机号：</div>
        <div class="field">
          <input  v-model="formData.contact" class="input" placeholder="请输入手机号" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地区：</div>
        <div class="field">
          <XtxCity @change="changeCity" :fullLocation="formData.fullLocation" placeholder="请选择所在地区"/>
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">详细地址：</div>
        <div class="field">
          <input v-model="formData.address" class="input" placeholder="请输入详细地址" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">邮政编码：</div>
        <div class="field">
          <input  v-model="formData.postalCode" class="input" placeholder="请输入邮政编码" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地址标签：</div>
        <div class="field">
          <input  v-model="formData.addressTags" class="input" placeholder="请输入地址标签，逗号分隔" />
        </div>
      </div>
    </div>
      </div>
      <template v-slot:footer>
        <XtxButton type="gray" style="margin-right:20px">取消</XtxButton>
        <XtxButton type="primary" @click="submit">确认</XtxButton>
      </template>
    </XtxDialog>
  </template>
  <script>
  import { reactive, ref } from 'vue'
  import {addAddress} from '@/api/order'
  import Message from '@/components/library/xtx-message'
  import {updateAddress} from '@/api/order'
  export default {
    name: 'AddressEdit',
    setup (props, {emit}) {
        const visibleDialog = ref(false)
        //父组件可调用此方法，显示隐藏对话框
        const open = (addAddress) => {
            visibleDialog.value = true
            if(addAddress.id) {
                //如果是修改 填充表单
                for(const key in formData) {
                    formData[key] = addAddress[key]
                }
            } else {
                //如果是添加
                //清除之前填写的数据
             // 传人{}的时候就是清空，否则就是赋值
            for (const key in formData) {
            if(key === 'isDefault') {
                formData[key] = 1
            } else {
                formData[key] = null
            }
        }
            }
            
        }

        //表单数据双向绑定
        const formData = reactive({
            id: null,
            receiver: null,
            contact: null,
            provinceCode: null,
            cityCode: null,
            countyCode: null,
            address: null,
            postalCode: null,
            addressTags: null,
            isDefault: 0,
            fullLocation: null
        })
        //拿到输入的数据，并赋值给formData
        const changeCity = (result) => {
            formData.provinceCode = result.provinceCode
            formData.countyCode = result.countyCode
            formData.cityCode = result.cityCode
            formData.fullLocation = result.fullLocation
        }


        //调用接口，添加地址
        const submit = () => {
            if(formData.id) {
                //修改地址
                updateAddress(formData).then(data => {
                    Message({ text: '修改收货地址成功', type: 'success' })
                    visibleDialog.value = false
                    emit('on-success', formData)
                })
            } else {
                //添加地址
                addAddress(formData).then(data => {
                Message({ text: '添加收货地址成功', type: 'success' })
               formData.id = data.result.id
               visibleDialog.value = false
                emit('on-success', formData)
                })
            }
            

        }




        return {visibleDialog, open, formData, changeCity, submit}
      
    }
  }
  </script>
  <style scoped lang="less">
  .address-edit {
    .xtx-dialog {
  :deep(.wrapper){
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
  
  }
  </style>